#!coding=utf-8
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from time import sleep




def swipeUp(driver,point_1,point_2):
    #定义屏幕向上滑动操作
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    #print u"屏幕分辨率为:", x, y
    l = (x,y)
    x1 = int(l[0]*0.5)
    y1 = int(l[1]*point_1)
    y2 = int(l[1]*point_2)
    driver.swipe(x1,y1,x1,y2,500)

def del_appdown_task(driver):
    #清空下载任务
    driver.find_element_by_id("com.sec.android.app.samsungapps:id/actionbar_download_icon").click()
    try:
        driver.find_element_by_id("com.sec.android.app.samsungapps:id/paused_list_cancel").click()
        driver.find_element_by_id("com.sec.android.app.samsungapps:id/menu_item_cancel").click()
    except:
        driver.find_element_by_id("com.sec.android.app.samsungapps:id/no_data_text")
    driver.back()

def clickappdown(driver):
    applistitem = driver.find_element_by_id("com.sec.android.app.samsungapps:id/download_btn_view")
    #print "Current page app number is:",len(applistitem)
    #for j in applistitem:
    for i in range(1,11):
        print i
        #点击单个应用进行安装
        applistitem.click()
        driver.implicitly_wait(30)
        #点击接受安装，如果出现异常提示，则跳过提示继续下载
        try:
            driver.find_element_by_id("com.sec.android.app.samsungapps:id/softkey_positive").click()
        except:
            driver.find_element_by_id("com.sec.android.app.samsungapps:id/body")
            driver.find_element_by_id("com.sec.android.app.samsungapps:id/positive").click()
            driver.find_element_by_id("com.sec.android.app.samsungapps:id/softkey_positive").click()
        driver.implicitly_wait(1)
        #如果apk很快下载完毕会提示安装，取消安装
        try:
            driver.find_element_by_id("com.android.packageinstaller:id/cancel_button").click()
        except:
            pass
        #取消当前下载任务
        driver.implicitly_wait(3)
        driver.find_element_by_id("com.sec.android.app.samsungapps:id/cancel_button").click()
        swipeUp(driver,0.5,0.43)
        sleep(1)
   



def soft(driver):
    driver.find_element_by_name("应用").click()    # 点击元素
    #driver.find_element_by_name("影音娱乐").click()
    driver.find_element_by_name("分类").click()
    driver.implicitly_wait(30)
    categorys = [u'工具',u'旅游',u'社交',u'音乐',u'美化',u'办公',u'摄影',u'阅读',u'生活',u'儿童',u'视频',u'系统',u'理财',u'购物',u'教育',u'出行',u'健康']
    for i in categorys:
        print "Current category is:",i
        try:
            driver.find_element_by_name(i).click()
        except:
            swipeUp(driver,0.5,0.2)
            sleep(1)
            driver.find_element_by_name(i).click()
        driver.find_element_by_name("排行").click()
        clickappdown(driver)
        driver.back()




def game(driver):
    driver.find_element_by_name("游戏").click()    # 点击元素
    #driver.find_element_by_name("影音娱乐").click()
    driver.find_element_by_name("分类").click()
    driver.implicitly_wait(30)
    categorys = [u'休闲益智',u'网络游戏',u'动作冒险',u'飞机射击',u'体育竞速',u'经营策略',u'棋牌中心',u'角色扮演']
    for i in categorys:
        print "Current category is:",i
        driver.find_element_by_name(i).click()
        driver.find_element_by_name("排行").click()
        clickappdown(driver)
        driver.back()




def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = '4.4.2'              # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = '127.0.0.1:62001'         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.sec.android.app.samsungapps'    # apk的包名
    desired_caps['appActivity'] = '.SamsungAppsMainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    driver.implicitly_wait(30)
    del_appdown_task(driver)
    soft(driver)
    game(driver)
    driver.quit()
